package top.infopub.mgr.fia.service.impl;


import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.fia.dao.ProBudgetDao;
import top.infopub.mgr.fia.dao.ProBudgetDetailDao;
import top.infopub.mgr.fia.domain.ProBudgetDomain;
import top.infopub.mgr.fia.service.ProBudgetService;
import top.infopub.mgr.fia.vo.ProBudgetVo;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.model.fia.ProBudget;
import top.infopub.model.fia.ProBudgetDetail;
import top.infopub.security.util.SessionUserUtils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


@Service
public class ProBudgetServiceImpl extends GenericServiceImpl<ProBudget, String> implements ProBudgetService {

    @Autowired
    private ProBudgetDao proBudgetDao;

    @Autowired
    private ProBudgetDetailDao proBudgetDetailDao;

    @Autowired
    private WsFlowProcessService wsFlowProcess;

    @Override
    public GenericDao<ProBudget, String> getDao() {
        return proBudgetDao;
    }

    @Override
    public Page<ProBudgetDomain> selectPageByDomainWhere(Page<ProBudgetDomain> page,
                                                         ProBudgetVo proBudgetVo) {
        // 权限内数据
        proBudgetVo.setVisibleProCodes(wsFlowProcess.proxyVisibleProCodes());
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ProBudgetDomain>)proBudgetDao.selectByDomainWhere(proBudgetVo);
    }

    @Override
    public ProBudgetVo selectById(String id) {
        return proBudgetDao.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addProBudget(ProBudgetVo proBudget, List<ProBudgetDetail> proBudgetDetails) {
        //插入预立项记录
        proBudgetDao.insertById(proBudget);

        //批量插入子项
        if (CollectionUtils.isNotEmpty(proBudgetDetails)) {
            proBudgetDetailDao.insertByBatch(proBudgetDetails);
        }

    }

    @Override
    public int count(Integer belongsYear,String proCode) {
        return proBudgetDao.count(belongsYear,proCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void udpProBudget(ProBudgetVo proBudgetVo) {
        
        ProBudget proBudget = proBudgetDao.selectByPrimaryKey(proBudgetVo.getId());
        BeanUtils.copyProperties(proBudgetVo, proBudget);
        proBudget.setProcessStatus("9");
        proBudget.setUptTime(new Date());
        proBudget.setUptBy(SessionUserUtils.getSessionUserId());
        this.updateBySelective(proBudget);

        //批量更新项目指标预算明细
        proBudgetDetailDao.updateByBatch(proBudgetVo.getBudgetDetails());
        
    }

}